import useAppStore from './appStore';
import useEditorStore from './editorStore';
import useProjectStore from './projectStore';
import useTerminalStore from './terminalStore';
import useSidebarStore from './sidebarStore';
import { useShallow } from 'zustand/react/shallow';

export { useAppStore, useEditorStore, useProjectStore, useTerminalStore, useSidebarStore };

export const useGlobalStores = () => ({
  app: useAppStore(),
  editor: useEditorStore(),
  project: useProjectStore(),
  terminal: useTerminalStore(),
  sidebar: useSidebarStore(),
});

export const useTheme = () => useAppStore(
  useShallow((state) => ({
    isDarkTheme: state.isDarkTheme,
    toggleTheme: state.toggleTheme,
    setTheme: state.setTheme,
  }))
);

export const useRunState = () => useProjectStore(
  useShallow((state) => ({
    isRunning: state.isRunning,
    runCurrentFile: state.runCurrentFile,
    updateCurrentFileContent: state.updateCurrentFileContent,
    stopCode: state.stopCode,
  }))
);

export const useEditorActions = () => useEditorStore(
  useShallow((state) => ({
    getCurrentCode: state.getCurrentCode,
    setEditorValue: state.setEditorValue,
    highlightErrorLine: state.highlightErrorLine,
    clearErrorHighlight: state.clearErrorHighlight,
    resetToDefault: state.resetToDefault,
  }))
);

export const useTerminalActions = () => useTerminalStore(
  useShallow((state) => ({
    createLine: state.createLine,
    addOutput: state.addOutput,
    addOutputLine: state.addOutputLine,
    addErrorLine: state.addErrorLine,
    addWarningLine: state.addWarningLine,
    addSystemLine: state.addSystemLine,
    clearOutput: state.clearOutput,
    parseOutput: state.parseOutput,
    toggleVisibility: state.toggleVisibility,
    showTerminal: state.showTerminal,
    hideTerminal: state.hideTerminal,
  }))
);

export const useTerminalVisibility = () => useTerminalStore(
  useShallow((state) => ({
    isVisible: state.isVisible,
    toggleVisibility: state.toggleVisibility,
    showTerminal: state.showTerminal,
    hideTerminal: state.hideTerminal,
  }))
);

export const useSidebarActions = () => useSidebarStore(
  useShallow((state) => ({
    isOpen: state.isOpen,
    width: state.width,
    toggleSidebar: state.toggleSidebar,
    setSidebarOpen: state.setSidebarOpen,
    setSidebarWidth: state.setSidebarWidth,
    isMobileDevice: state.isMobileDevice,
  }))
);

export const useFileTree = () => useSidebarStore(
  useShallow((state) => ({
    expandedFolders: state.expandedFolders,
    selectedFileId: state.selectedFileId,
    toggleFolder: state.toggleFolder,
    expandFolder: state.expandFolder,
    collapseFolder: state.collapseFolder,
    setSelectedFile: state.setSelectedFile,
  }))
);

export const useFileOperations = () => useSidebarStore(
  useShallow((state) => ({
    fileOperations: state.fileOperations,
  }))
);
